package com.faryangsh.scheduleTask.Timer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 杨志远 on 2017/6/7
 * email faryangsh@163.com.
 */
@Slf4j
@Service
public class Timer {
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    @Scheduled(cron = "0/2 * * * * ?")
    public void checkSkuFailedFromRedis() {

        log.info("timer stat");
        log.info(" send to kafka");
        kafkaTemplate.send("test-topic12","kakkkf");
        //使用异步线程池做具体的事情
            taskExecutor.execute(()->{
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("2--Thread: "+Thread.currentThread().getName()+", start timerTask,time: "+ dateFormat.format(new Date()));
            });
    }



   /* @Bean(destroyMethod = "shutdown")
    public Executor taskScheduler() {
        return Executors.newScheduledThreadPool(5);
    }*/
}
